package www.geek.filemanager.provider;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

import java.util.ArrayList;
import java.util.List;

import www.geek.filemanager.bean.AppInfo;

/**
 * description	: 提供应用信息的Provider
 *
 * @author : zhanggeng
 * @date : 15/8/5
 * @version     : v1.0
 * ----------修订历史-----------
 */
public class AppInfoProvider {

    //应用是否自启动
    private boolean appStartSelf;
    //应用是否正在运行
    private boolean isRunning;

    public static List<AppInfo> getAppInfos(Context context){

        PackageManager pm = context.getPackageManager();

        List<PackageInfo> infos = pm.getInstalledPackages(0);
        List<AppInfo> appInfos = new ArrayList<>();

        for(PackageInfo info:infos){

            AppInfo appInfo = new AppInfo();

            String packageName = info.packageName;
            String appName = info.applicationInfo.loadLabel(pm).toString();
            String appVersion = info.versionName;
            Drawable icon = info.applicationInfo.loadIcon(pm);

            appInfo.setAppName(appName);
            appInfo.setAppVersion(appVersion);
            appInfo.setPackageName(packageName);
            appInfo.setAppIcon(icon);

            int flag = info.applicationInfo.flags;

            if((flag& ApplicationInfo.FLAG_SYSTEM)==0){
                appInfo.setIsUserApp(true);
            }else{
                appInfo.setIsUserApp(false);
            }

            if((flag&ApplicationInfo.FLAG_EXTERNAL_STORAGE)==0){
                appInfo.setInRom(true);
            }else{
                appInfo.setInRom(false);
            }

            appInfos.add(appInfo);

            //TODO: 添加获取自启动、是否正在运行的代码，尚未完善

        }
        return appInfos;
    }

}
